package main

import (
	"context"
	"fmt"
	"github.com/apache/thrift/lib/go/thrift"
	"rpc/thrift/t1/user"
)

var ctx = context.Background()

func main() {
	var transport thrift.TTransport
	var err error

	// 传输方式（需要与服务端一致）
	transport, err = thrift.NewTSocket("localhost:9000")
	if err != nil {
		fmt.Printf("open socket err, %s\n", err)
		return
	}

	// 传输协议（需要与服务端一致）
	protocolFactory := thrift.NewTBinaryProtocolFactoryDefault()
	iProtocol := protocolFactory.GetProtocol(transport)
	oProtocol := protocolFactory.GetProtocol(transport)
	tClient := thrift.NewTStandardClient(iProtocol, oProtocol)

	// 实际业务
	userClient := user.NewUserClient(tClient) // NewUserClient User就是我们的User，随我们的服务变化
	if err := transport.Open(); err != nil {
		fmt.Printf("Error opening socket to :9092 %s", err)
		return
	}
	defer transport.Close()

	// 调用rpc服务提供的方法
	res, err := userClient.GetName(ctx,1)
	if err != nil {
		fmt.Printf("get user err, %s", err)
		return
	}
	fmt.Printf("%s", res)
}